﻿#region Copyright
//————————————————————————————————————————//
//	创建人员(Creator)    ：Huangyd
//	创建日期(Date)       ：2012-07-04
//  联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
//  描    述(Description)：
//
//	Copyright (C) 2012 ajayumi 版权所有
//————————————————————————————————————————//
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ajayumi.Platform.Win.DataGridView
{
    /// <summary>
    /// 时间列
    /// </summary>
    public class DataGridViewTimerColumn : DataGridViewColumn
    {
        internal const string CUSTOM_FORMAT = "HH:mm";

        public DataGridViewTimerColumn()
            : base(new DataGridViewTimerCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a CalendarCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(DataGridViewTimerCell)))
                {
                    throw new InvalidCastException("Must be a DataGridViewTimerCell");
                }
                base.CellTemplate = value;
            }
        }
    }

    public class DataGridViewTimerCell : DataGridViewTextBoxCell
    {
        public DataGridViewTimerCell()
            : base()
        {
            // Use the short date format.
            this.Style.Format = DataGridViewTimerColumn.CUSTOM_FORMAT;
        }

        public override void InitializeEditingControl(int rowIndex, object
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
                dataGridViewCellStyle);
            DataGridViewTimerEditingControl ctl = DataGridView.EditingControl as DataGridViewTimerEditingControl;

            DateTime dateTime = DateTime.Now;
            if (null != this.Value && !string.IsNullOrEmpty(this.Value.ToString()))
            {
                DateTime.TryParse(this.Value.ToString(), out dateTime);
            }
            ctl.Value = dateTime;
            ctl.ShowUpDown = true;
        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that CalendarCell uses.
                return typeof(DataGridViewTimerEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that CalendarCell contains.
                return typeof(DateTime);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                // Use the current date and time as the default value.
                return DateTime.Now;
            }
        }
    }

    internal class DataGridViewTimerEditingControl : DataGridViewCalendarEditingControl
    {
        protected override void InitCustomFormat()
        {
            if (string.IsNullOrEmpty(this.CustomFormat))
            {
                this.CustomFormat = DataGridViewTimerColumn.CUSTOM_FORMAT; ;
            }
        }
    }
}
